<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div class="col-md-12">
	<div class="portlet-title">
		<div class="caption">
			<i class="fa fa-gift"></i>关联客户
		</div>
   </div>
   
  <div class="portlet-body form col-md-12">
  	<form id="staffForm" class="form-horizontal form-bordered" action="/decidedzone/list" method="post">
	 <label class="col-md-3 control-label">已关联的客户:</label>
        <div class="col-md-4">
        	<div class="input-icon right">
	        	<i class="fa"></i>
	          <select id="customerId" class="form-control select2" 
	          		style="padding: 0;" required multiple >
	          		<c:forEach items="${customers }" var="customer">
	          			<option value="${customer.id }"<c:if test="${customer.hasAss }">selected</c:if>>${customer.name }</option>
	          		</c:forEach>
	          </select>
	        </div>
        </div>
       </form>
      </div>
		
	  <div class="btn-div col-md-4 col-md-offset-4" >
	  		<br><br>
			<div class="div-left">
				<button type="button" class="btn btn-warning" onclick="relationCustomer('/decidedzone/relationCustomers')">
					<i class="fa fa-plus"></i> <spring:message code="btn.relation"></spring:message>
				</button>
				
				<button type="button" class="btn btn-warning" onclick="cancel()">
					<i class="fa fa-plus"></i> <spring:message code="btn.return"></spring:message>
				</button>
			</div>
		</div>
	</div>
	
	<div class="panel-content"></div>
	<input type="hidden" name="decidedzoneId" id="decidedzoneId" value="${param.id }"/>
<script type="text/javascript">
	$(function(){
		$("#customerId").select2({
			allowClear: true
		});
	});
	
	function relationCustomer(url) {
		var customerIdsArr = new Array();
		var customerId = "";
		$("#customerId option:selected").each(function(i){
			customerId = $(this).val();
			customerIdsArr.push(customerId);
		})
		var customerIds = customerIdsArr.join(",");
		var decidedzoneId = $("#decidedzoneId").val();
		url = "${webRoot}"+url;
		var data = {"customerIds":customerIds,"decidedzoneId":decidedzoneId};
		
		$.post(url, data, function(result){
			if(result.code == 0) {
				layer.msg(result.message, {shift: 1});
				setTimeout(function(){
					search();
					$(".panel-search").show();
				}, 1200);
			}else{
				layer.msg(result.message, {shift: 6});
			}
		})
	}
	
</script>